<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\UserCommission;
use App\Traits\Controllers\IndexTraits;
use App\Transformers\UserCommissionTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class UserBrokerageController extends Controller
{
    use IndexTraits;

    public $model = UserCommission::class;

    public $transformer = UserCommissionTransformer::class;

    //佣金记录列表
    public function index(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        //处理传来得时间
        $time = $request->input('date');
        //需要判断是否传入时间的搜索条件
        if (!empty($time)) {
            $time = explode('-', $time);
            switch ($time) {
                case 'today' :
                    $time = [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()];
                    break;
                case 'yesterday' :
                    $time = [Carbon::now()->yesterday(), Carbon::now()->startOfDay()];
                    break;
                case 'week':
                    $time = [Carbon::now()->subDays(7), Carbon::now()->startOfDay()];
                    break;
                case 'thirty':
                    $time = [Carbon::now()->subDays(30), Carbon::now()->startOfDay()];
                    break;
                case 'month':
                    $time = [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()];
                    break;
                case 'year':
                    $time = [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()];
                    break;
            }
        }
        //处理传来的用户的范围
        $min = $request->input('price_min');  //最小佣金
        $max = $request->input('price_max');  //最大佣金
        //进行查询
        $data = $this->getQuery()
            ->pimp()
            ->when($time, function ($query) use ($time) {  //根据时间进行条件搜索
                $query->whereBetween('created_at', $time);
            })
            ->when($min, function ($query) use ($min) {   //根据佣金范围进行搜索
                $query->where('account_amount', '>=', $min);
            })
            ->when($max, function ($query) use ($max) {
                $query->where('account_amount', '<=', $max);
            })
            ->GroupBy('user_id')  //进行分组
            //进行统计查询
            ->selectRaw('user_id, sum(account_amount) as account_amount,
                                  sum(withdrawal_amount) as withdrawal_amount,
                                  sum(account_amount) as total_amount,
                                  max(created_at) as created_at,
                                  max(id) as id'
            )
            ->with('user:id,balance,nickname')  //包含用户的信息
            ->paginate($this->getPageLimit());
        //进行分页返回
        return $this->response->paginator($data, resolve($this->transformer));
    }

    //查询单个佣金记录中的用户的信息
    public function show_user($id)
    {
        $user = $this->getQuery()
            ->find($id)  //查询传来id的信息
            ->user() //使用模型关联进行查询相关的用户的信息
            ->with('promoter:id,real_name')   //查询用户的上级推广人的姓名
            ->first();
        $data = $user->toArray();  //将查询到的信息转换为数据格式
        $data['created_at'] = $user->created_at->format("Y-m-d H:i:s"); // 将carbon的时间格式进行格式话
        //返回查询到的数据
        return $this->response->array($data);
    }

    public function show($id, Request $request)
    {
        $star = $request->input('start');
        $end = $request->input('end');
        $data = $this->getQuery()->find($id);  //获取传来的信息
        $data = $this->getQuery()
            ->when($star, function ($query) use ($star, $end) {
                $query->whereBetween('created_at', [$star, $end]);
            })
            ->select('id', 'account_amount', 'created_at', 'remarks')
            ->where('user_id', $data->user_id) // 查询所有 user_id 等于获取到的用户id
            ->paginate($this->getPageLimit());  //进行分页显示
        //将数据进行格式化返回
        return $this->response->paginator($data, resolve($this->transformer));
    }
}
